import urllib2
import sys
sys.path.insert(0, 'libs')
from bs4 import BeautifulSoup
from google.appengine.ext import db
from google.appengine.api import mail

class MyData(db.Model):
    myKey = db.StringProperty(required=True)
    myValue = db.StringProperty(required=True)

def getData():
    opener = urllib2.build_opener()
    infile = opener.open('http://police.uk/data')
    page = infile.read()
    soup = BeautifulSoup(page)
    intro = soup.find(id="intro").findNext("h3")
    data = MyData(myKey="pageextract",myValue=str(intro))
    return data
    
def getdbData():
    q = db.Query(MyData)
    return q.get()

def updated():
    livepagedata = getData()
    storeddata = getdbData()

    if storeddata and (str(livepagedata.myValue) != str(storeddata.myValue)):
    	storeddata.myValue = livepagedata.myValue
    	storeddata.put()
        return True
    else:
        if storeddata:
            storeddata.put()
        else:
            livepagedata.put()
        return False

if updated():
    print "page has changed"
    message = mail.EmailMessage(sender="jonathan.lakethomas@gmail.com",
                                subject="police.uk page has been updated")

    message.to = "Jonathan Lake-Thomas <jonathan.lakethomas@btinternet.com>"
    message.body = """ New data has been added to the police.uk page.
    """

    message.send()
        
        